import router from '../router/index.js'
import menuConfig from '../../menuConfig.js'
function getMenuRoute(route) {
  return []
}
const menusState = {
  state: () => ({
    menusList: [],
    menusBread: [],
  }),
  mutations: {
    updataNowShow(state, index) {
      state.menusList.forEach(item => {
        item.now = false
      })
      state.menusList[index].now = true
    },
    setMenu(state, route) {
      if (route.params) {
        if (!route.name) {
          new Error('跳转路由name属性不存在')
        }
        router.push({ name: route.name, params: route.params })
      } else if (route.query) {
        router.push({ path: route.url, query: route.query })
      } else {
        router.push(route.url)
      }
      if (route.pushMenu === false) {
        return
      }
      state.menusBread = getMenuRoute(route)
      let hasIndex = state.menusList.findIndex(item => {
        return item.id == route.id
      })
      if (hasIndex > -1) {
        this.commit('updataNowShow', hasIndex)
      } else {
        state.menusList.push(route)
        this.commit('updataNowShow', state.menusList.length - 1)
      }
    },
    deleteMenu(state, index) {
      state.menusList.splice(index, 1)
      if (state.menusList.length) {
        this.commit('setMenu', state.menusList[state.menusList.length - 1])
      } else {
        this.commit('setMenu', { url: '/', pushMenu: false })
      }
    },
  },
  actions: {},
  getters: {
    menus(state) {
      return state.menusList
    },
    menusBread(state) {
      return state.menusBread
    },
  },
}
export default menusState
